package com.work.device.controller;

import com.github.pagehelper.Page;
import com.work.device.pojo.Device;

import com.work.device.pojo.MyDevice;
import com.work.device.service.MyDeviceService;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/webDevice")
public class MyDeviceController {

    @Autowired
    private MyDeviceService mydeviceService;


    /**
     * 查询所有设备
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<MyDevice> devices = mydeviceService.findAll();
            return new Result(true, StatusCode.FIND_Ok, "查询设备成功", devices);

        } catch (Exception e) {
            return new Result(true, StatusCode.FIND_ERROR, "查询设备失败");
        }

    }


    /**
     * 用户自行采购添加设备
     *
     * @param map
     * @return
     */
    @PostMapping("/addMyDevice")
    public Result addMyDevice(@RequestBody Map<String, Object> map) {
        mydeviceService.add(map);
        return new Result(true, StatusCode.ADD_OK, "添加成功");
    }

    /**
     * 根据设备修改设备的运行状态
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody MyDevice mydevice) {
        mydeviceService.updateById(mydevice);
        return new Result(true, StatusCode.UPDATE_Ok, "修改设备状态成功");

    }

    /**
     * 根据id删除设备
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        try {
            Integer status = mydeviceService.delete(id);
            if (status == 1) {
                return new Result(true, StatusCode.DELETE_OK, "删除设备成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.DELETE_ERROR, "删除设备失败");

    }

}
